package drone.iqgc.service.entity.core ;

import java.math.BigDecimal ;
import java.util.HashMap ;
import java.util.List ;
import java.util.Map ;
import java.util.Objects ;

import org.springframework.beans.factory.annotation.Autowired ;
import org.springframework.stereotype.Service ;
import org.springframework.transaction.annotation.Transactional ;

import com.github.pagehelper.PageHelper ;

import drone.basis.base.BaseResult ;
import drone.basis.meta.MethodMeta ;
import drone.basis.util.PageUtil ;
import drone.basis.util.StringUtil ;
import drone.facade.model.entity.core.CoreMissionEntity ;
import drone.facade.model.entity.core.CoreMissionEntity.CoreMissionField ;
import drone.iqgc.dao.entity.core.ICoreMissionDao ;

@Service  
@Transactional
public class CoreMissionService
{

	@Autowired
	private ICoreMissionDao coreMissionDao ;

	@MethodMeta ( name = "是否存在" , description = "根据主键判断是否存在" )
	public Boolean exist (
			String primaryKey
	) throws Exception
	{
		if ( Objects.isNull ( fetch ( primaryKey ) ) )
		{
			return new Boolean ( false ) ;
		}
		return new Boolean ( true ) ;
	}

	@MethodMeta ( name = "是否存在" , description = "根据条件判断是否存在" )
	public Boolean contain (
			Map < String , String > paramMap
	) throws Exception
	{
		if ( Objects.isNull ( single ( paramMap ) ) )
		{
			return new Boolean ( false ) ;
		}
		return new Boolean ( true ) ;
	}

	@MethodMeta ( name = "获取单条记录" , description = "根据主键获取唯一数据记录" )
	public CoreMissionEntity fetch (
			String primaryKey
	) throws Exception
	{
		if ( StringUtil.isEmpty ( primaryKey ) )
		{
			return null ;
		}
		return coreMissionDao.obtain ( primaryKey ) ;
	}

	@MethodMeta ( name = "获取单条记录" , description = "根据条件获取唯一数据记录" )
	public CoreMissionEntity single (
			Map < String , String > paramMap
	) throws Exception
	{
		if ( Objects.isNull ( paramMap ) )
		{
			return null ;
		}
		PageHelper.startPage ( 1 , 1 , false ) ;
		return coreMissionDao.unique ( paramMap ) ;
	}

	@MethodMeta ( name = "获取单条记录" , description = "随机获取唯一数据记录" )
	public CoreMissionEntity dynamic (
			Map < String , String > paramMap
	) throws Exception
	{
		if ( Objects.isNull ( paramMap ) )
		{
			return null ;
		}
		return coreMissionDao.random ( paramMap ) ;
	}

	@MethodMeta ( name = "保存记录" , description = "根据对象主键字段在数据库内是否已经存在来进行更新或者插入操作,返回唯一数据记录" )
	public CoreMissionEntity persist (
			CoreMissionEntity coreMissionEntity
	) throws Exception
	{
		if ( Objects.isNull ( coreMissionEntity ) )
		{
			return null ;
		}

		CoreMissionEntity _coreMissionEntity = fetch ( coreMissionEntity.primaryValue ( ) ) ;

		if ( Objects.isNull ( _coreMissionEntity ) )
		{
			coreMissionDao.insert ( coreMissionEntity ) ;
			return coreMissionEntity ;
		}

		coreMissionDao.update ( coreMissionEntity ) ;
		return fetch ( coreMissionEntity.primaryValue ( ) ) ;

	}

	@MethodMeta ( name = "批量插入" , description = "批量写入数据记录" )
	public Integer plural (
			List < CoreMissionEntity > paramList
	) throws Exception
	{
		if ( Objects.isNull ( paramList ) )
		{
			return new Integer ( 0 ) ;
		}
		Integer result = coreMissionDao.batch ( paramList ) ;
		return result ;
	}

	@MethodMeta ( name = "批量更新" , description = "批量写入数据记录" )
	public Integer alter (
			Map < String , String > paramMap
	) throws Exception
	{
		if ( Objects.isNull ( paramMap ) )
		{
			return new Integer ( 0 ) ;
		}
		Integer result = coreMissionDao.change ( paramMap ) ;
		return result ;
	}

	@MethodMeta ( name = "删除记录" , description = "根据主键删除对应记录" )
	public Integer remove (
			String primaryKey
	) throws Exception
	{
		if ( StringUtil.isEmpty ( primaryKey ) )
		{
			return new Integer ( 0 ) ;
		}

		Integer result = coreMissionDao.delete ( primaryKey ) ;

		return result ;
	}

	@MethodMeta ( name = "删除记录" , description = "根据条件删除对应记录" )
	public Integer expunge (
			Map < String , String > paramMap
	) throws Exception
	{
		if ( Objects.isNull ( paramMap ) )
		{
			return new Integer ( 0 ) ;
		}
		if ( paramMap.size ( ) == 0 )
		{
			throw new Exception ( "no limit delete exception" ) ;
		}

		Integer result = coreMissionDao.clear ( paramMap ) ;

		return result ;
	}

	@MethodMeta ( name = "合计字段" , description = "根据条件合计指定字段的数字之和" )
	public BigDecimal summary (
			String polyInfo ,
			Map < String , String > paramMap
	) throws Exception
	{
		if ( Objects.isNull ( paramMap ) )
		{
			paramMap = new HashMap < String , String > ( ) ;
		}
		if ( StringUtil.isEmpty ( polyInfo ) )
		{
			return new BigDecimal ( 0 ) ;
		}
		paramMap.put ( "poly" , " sum( " + CoreMissionField.valueOf ( polyInfo ).getName ( ) + ") " ) ;
		BigDecimal result = coreMissionDao.total ( paramMap ) ;
		if ( Objects.isNull ( result ) )
		{
			return new BigDecimal ( 0 ) ;
		}
		return result ;
	}

	@MethodMeta ( name = "平均字段" , description = "根据条件计算指定字段的数字平均值" )
	public BigDecimal mean (
			String averInfo ,
			Map < String , String > paramMap
	) throws Exception
	{
		if ( Objects.isNull ( paramMap ) )
		{
			paramMap = new HashMap < String , String > ( ) ;
		}
		if ( StringUtil.isEmpty ( averInfo ) )
		{
			return new BigDecimal ( 0 ) ;
		}
		paramMap.put ( "aver" , " avg( " + CoreMissionField.valueOf ( averInfo ).getName ( ) + ") " ) ;
		BigDecimal result = coreMissionDao.average ( paramMap ) ;
		if ( Objects.isNull ( result ) )
		{
			return new BigDecimal ( 0 ) ;
		}
		return result ;
	}

	@MethodMeta ( name = "汇总字段" , description = "根据条件汇总指定字段的数量" )
	public Integer record (
			Map < String , String > paramMap
	) throws Exception
	{
		return coreMissionDao.count ( paramMap ) ;
	}

	@MethodMeta ( name = "查询数据" , description = "根据条件,分页和排序进行查询数据记录" )
	public BaseResult < CoreMissionEntity > search (
			String sortField ,
			Boolean sortOrder ,
			PageUtil pageUtil ,
			Map < String , String > paramMap
	) throws Exception
	{
		if ( Objects.isNull ( paramMap ) )
		{
			paramMap = new HashMap < String , String > ( ) ;
		}
		if ( ! StringUtil.isEmpty ( sortField ) )
		{
			paramMap.put ( "sort" , " order by " + CoreMissionField.valueOf ( sortField ).getName ( ) + " " + ( ( ( ! StringUtil.isEmpty ( sortOrder ) && sortOrder ) ) ? "asc" : "desc" ) ) ;
		}
		PageHelper.startPage ( pageUtil.getPageIndex ( ) , pageUtil.getPageSize ( ) , false ) ;
		List < CoreMissionEntity > resultList = coreMissionDao.query ( paramMap ) ;
		pageUtil.setPageRecord ( coreMissionDao.count ( paramMap ) ) ;
		pageUtil.allocation ( ) ;
		return new BaseResult < CoreMissionEntity > ( resultList , pageUtil ) ;
	}

}
